{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 利用textblob进行情感分析\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " 安装textblob\n",
    "https://github.com/sloria/TextBlob\n",
    "    \n",
    "> pip install -U textblob\n",
    "\n",
    "> python -m textblob.download_corpora"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-30T12:28:01.937210Z",
     "start_time": "2020-10-30T12:28:01.930918Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "The titular threat of The Blob has always struck me as the ultimate movie\n",
      "monster: an insatiably hungry, amoeba-like mass able to penetrate\n",
      "virtually any safeguard, capable of--as a doomed doctor chillingly\n",
      "describes it--\"assimilating flesh on contact. 0.06000000000000001 0.605\n",
      "Snide comparisons to gelatin be damned, it's a concept with the most\n",
      "devastating of potential consequences, not unlike the grey goo scenario\n",
      "proposed by technological theorists fearful of\n",
      "artificial intelligence run rampant. -0.34166666666666673 0.7666666666666666\n"
     ]
    }
   ],
   "source": [
    "from textblob import TextBlob\n",
    "\n",
    "text = '''\n",
    "The titular threat of The Blob has always struck me as the ultimate movie\n",
    "monster: an insatiably hungry, amoeba-like mass able to penetrate\n",
    "virtually any safeguard, capable of--as a doomed doctor chillingly\n",
    "describes it--\"assimilating flesh on contact.\n",
    "Snide comparisons to gelatin be damned, it's a concept with the most\n",
    "devastating of potential consequences, not unlike the grey goo scenario\n",
    "proposed by technological theorists fearful of\n",
    "artificial intelligence run rampant.\n",
    "'''\n",
    "\n",
    "blob = TextBlob(text)\n",
    "\n",
    "for sentence in blob.sentences:\n",
    "    print(sentence, sentence.sentiment.polarity, sentence.sentiment.subjectivity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-30T12:28:30.425501Z",
     "start_time": "2020-10-30T12:28:30.408508Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I feel happy this morning ： neg\n",
      "Oh I love my friend ： pos\n",
      "not like that man ： neg\n",
      "this hourse is not great ： neg\n",
      "your song is annoying ： neg\n"
     ]
    }
   ],
   "source": [
    "from textblob.classifiers import NaiveBayesClassifier\n",
    "\n",
    "train=[\n",
    "        ('I love this car','pos'),\n",
    "        ('This view is amazing','pos'),\n",
    "        ('I feel great','pos'),\n",
    "        ('I am so excited about the concert','pos'),\n",
    "        (\"He is my best friend\",'pos'),\n",
    "        ('I do not like this car','neg'),\n",
    "        ('This view is horrible','neg'),\n",
    "        (\"I feel tired this morning\",'neg'),\n",
    "        ('I am not looking forward to the concert','neg'),\n",
    "        ('He is an annoying enemy','neg')\n",
    "]\n",
    "\n",
    "test=[\n",
    "        ('I feel happy this morning','pos'),\n",
    "        ('Oh I love my friend','pos'),\n",
    "        ('not like that man','neg'),\n",
    "        (\"this hourse is not great\",'neg'),\n",
    "        ('your song is annoying','neg')\n",
    "]\n",
    "\n",
    "cl=NaiveBayesClassifier(train)\n",
    "\n",
    "for sentence in test:\n",
    "    print(sentence[0],'：',cl.classify(sentence[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## SnowNLP\n",
    "\n",
    "https://pypi.org/project/snownlp/\n",
    "\n",
    "https://github.com/isnowfy/snownlp\n",
    "\n",
    "SnowNLP介绍：是一个python写的类库，可以方便的处理中文文本内容，是受到了TextBlob的启发而写的，由于现在大部分的自然语言处理库基本都是针对英文的，于是写了一个方便处理中文的类库，并且和TextBlob不同的是，这里没有用NLTK，所有的算法都是自己实现的，并且自带了一些训练好的字典。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-29T09:15:48.119323Z",
     "start_time": "2020-10-29T09:15:40.315627Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing /Users/datalab/Library/Caches/pip/wheels/f3/81/25/7c197493bd7daf177016f1a951c5c3a53b1c7e9339fd11ec8f/snownlp-0.12.3-cp37-none-any.whl\n",
      "Installing collected packages: snownlp\n",
      "Successfully installed snownlp-0.12.3\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install snownlp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-10-30T12:29:18.650391Z",
     "start_time": "2020-10-30T12:29:16.249043Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9769551298267365"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from snownlp import SnowNLP\n",
    "\n",
    "s = SnowNLP(u'这个东西真心很赞')\n",
    "\n",
    "s.sentiments    # 0.9769663402895832 positive的概率"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
